/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.house.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.util.BeanJsonUtil;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IManageAreaService;
import com.ce.pms.house.vo.CompanyVO;
import com.ce.pms.house.vo.ManageOrgVO;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：管理处ACTION
 * @author 罗军林 创建日期：2008-7-9
 */
public class ManageOrgAction extends PmsBaseAction{
	private IManageAreaService manageAreaService;


	/**
	 * 功能描述:初始化管理区域设置的主页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initCompanyManageOrgMain(){

	}

	/**
	 * 功能描述:初始化管理处主页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-9<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initManageOrgMain(@Read(key="manageOrgId") Long manageOrgId){
		ManageOrgVO manageOrgVO= manageAreaService.findManageOrgById(manageOrgId);
		setAttribute("manageOrgVO", manageOrgVO);
	}

	/**
	 * 功能描述:初始化物业公司主页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-11<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initCompanyMain(@Read(key="companyId") Long companyId){
		CompanyVO companyVO= manageAreaService.findCompanyById(companyId);
		setAttribute("companyVO", companyVO);
	}

	/**
	 * 功能描述:初始化管理处添加页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initManageOrgAdd(){
		setCompanyListToRequest();
		setAttribute("isEdit", false);
	}



	/**
	 * 功能描述:初始化管理处修改页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initManageOrgEdit( @Read (key="manageOrgId") Long manageOrgId ){
		ManageOrgVO manageOrgVO = manageAreaService.findManageOrgById(manageOrgId);

		setCompanyListToRequest();
		setAttribute("manageOrgVO", manageOrgVO);
		setAttribute("isEdit", true);
	}

	/**
	 * 功能描述:初始化管理处列表页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initManageOrgList(	@Read(key="companyId") Long companyId,
									@Read(key = "ec_crd",defaultValue="15") int pageSize,
									@Read(key = "ec_p",defaultValue="1")	int pageNo)
	{
		Page page=manageAreaService.queryPageManageOrg(companyId, pageNo, pageSize);
		setAttribute("manageOrgList", page.getResult());
		setAttribute("companyId", companyId);
		setAttribute("totalRows", new Integer( (int) page.getTotalCount()));
	}

	/**
	 * 功能描述:增加一个管理处
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws Throwable
	 */
	public void addManageOrg(@Read ManageOrgVO manageOrgVO) throws Exception{
		manageOrgVO.setCreator(this.getUserId());
		ManageOrgVO manageOrg = manageAreaService.addManageOrg(manageOrgVO);
		String jsonStr=BeanJsonUtil.BeanToJsonStr(manageOrg);
		renderText(jsonStr);
	}

	/**
	 * 功能描述:修改一个管理处
	 * @param manageOrgVO
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws Exception
	 */
	public void editManageOrg(@Read ManageOrgVO manageOrgVO) throws Exception{
		manageOrgVO.setCreator(this.getUserId());
		ManageOrgVO manageOrg=manageAreaService.editManageOrg(manageOrgVO);
		String jsonStr=BeanJsonUtil.BeanToJsonStr(manageOrg);
		renderText(jsonStr);
	}

	/**
	 * 功能描述:删除一个管理处
	 * @param manageOrgId
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void deleteManageOrg( @Read (key="manageOrgId") Long manageOrgId ){
		try{
			manageAreaService.deleteManageOrg(manageOrgId);
		}catch (Throwable e) {
			throw new BusinessException("common:delete_error_exception",BusinessException.WARNING);
		}
	}


	public void listManageOrg(){

	}

	/**
    * 功能描述:生成物业公司及其下的管理处树结构XML
    *
    * Create author:罗军林<br>
    * Create on:2008-7-10<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    * @throws Exception
    */
	public void getCompanyTreeData() throws Exception{
		List<Map> companyList = this.manageAreaService.queryCompanyManageOrgTreeList();
		Document doc=generationTreeXML(companyList);
		writeXMLtoClient(doc);
   }

	/**
	 * 功能描述:校验管理处名称的唯一性<br>
	 * @param manageOrgByName
	 * @param oldName<br>
	 * Create author:罗军林<br>
	 * Create on:2008-7-15<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkManageOrgName(	@Read (key="name") String manageOrgByName,
									@Read (key="oldName") String oldName){
		Long result=manageAreaService.checkManageOrgByName(manageOrgByName, oldName);
		if(result<=0){
			renderText("true");
		}else{
			renderText("已经存在");
		}
	}

	/*************私有方法*********************************************************/

	/** 功能描述:将生成的DOCUMENT以XML的形式输出到客户端
	 * @param doc
	 * @throws IOException
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void writeXMLtoClient(Document doc) throws IOException {
		HttpServletResponse response = getResponse();
		response.setContentType("text/xml;charset=UTF-8");
		XMLOutputter XMLOut = new XMLOutputter();
		PrintWriter out = response.getWriter();
		XMLOut.output(doc, out);
		out.flush();
	}


	/** 功能描述:生成物业公司列表DOCUMENT
	 * @param companyList
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Document generationTreeXML(List<Map> companyList) {
		Document doc=new Document();
		Element rootElement = generationRootNode();
		for (Map map : companyList) {
			CompanyVO companyVO = (CompanyVO)map.keySet().toArray()[0];
			if(null==companyVO) break;
			Element item = generationNode(companyVO.getKeyword(),companyVO.getName());
			List<ManageOrgVO> manageOrgList= (List<ManageOrgVO>) map.get(companyVO);
			if(null != manageOrgList){
				for (ManageOrgVO itemVO : manageOrgList) {
					item.addContent(generationNode(itemVO.getKeyword(),itemVO.getName()));
				}
			}
			rootElement.addContent(item);
		}
		return doc.addContent(rootElement);
	}

	/** 功能描述:生成一个根结点
	 * @return
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Element generationRootNode() {
		Element rootElement = new Element("tree");
		Attribute rootAttri = new Attribute("id", "0");
		rootElement.setAttribute(rootAttri);
		rootElement.setAttribute("select", "true");
		rootElement.setAttribute("tooltip", "根接点");
		return rootElement;
	}

	/** 功能描述:生成一个节点
	 * @param id
	 * @param text
	 * @return
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Element generationNode(Long id,String text) {
		Element item = new Element("item");
		item.setAttribute("id",String.valueOf(id));
		item.setAttribute("text", text);
		item.setAttribute("im1", "root.gif");
		item.setAttribute("im2", "root.gif");
		return item;
	}

	/**
	 * 功能描述:功能描述:返回所以有公司列表，并将列表信息以"companyList"名写入到request缓存中
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-14<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void setCompanyListToRequest() {
		List<CompanyVO> companyList=manageAreaService.findAllCompany();
		setAttribute("companyList", companyList);
	}

	/**
	 * @return the manageAreaService
	 */
	public IManageAreaService getManageAreaService() {
		return manageAreaService;
	}

	/**
	 * @param manageAreaService the manageAreaService to set
	 */
	public void setManageAreaService(IManageAreaService manageAreaService) {
		this.manageAreaService = manageAreaService;
	}



}
